<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
// Get Form Field in the database
if(isset($_POST['action'])){
    if($_POST['action']=="getFormField"){
        $numCols = $_POST['numCols']; // number of created columns
        $setCols = $_POST['setCols']; // Column Title
        $split_setCols = explode(",", $setCols); // remove delimiters
        $getFormField = $db->query("SELECT * FROM tbform f LEFT JOIN tbfield_output fo on f.id=fo.form_id where f.company_id = '$company_id'","array");
        echo "<br />";
            echo ' <select class="input-small selectLabel" multiple="multiple" id="selectLabel" data-enem-col="">';
                foreach($getFormField as $field){
                    echo '<option data-field-id="'.$field['id'].'">'.$field['f_label'].'</option>';
                }
            echo '</select>';
            echo '<br />';
            for ($n = 1; $n <= $numCols; $n++){
                echo 'Value of '.$split_setCols[$n-1].': * <br />';
                echo '<div class="formRadio">';
                    echo '<input type="radio" id="enabledCol_'.$n.'" data-enem-col="'.$n.'" class="radio enabledCol" name="radio"/>';
                echo '</div>';
                echo '<input type="text" name="dataFieldSet[]" disabled="disabled" id="insertSelectedCol_'.$n.'" data-company-id="'.$company_id.'" class="input-small insertSelectedCol" />';
                echo '<span class="fornav" id="fornav2">&nbsp;<i class="icon-minus-sign" id="removeSelectedField" data-enem-col="'.$n.'"></i></span>';
                echo '<br />';
                
            }
            
        echo ' <input type="button" class="btn" value="Submit" id="saveModSettings"/>';
    }elseif($_POST['action']=="saveFormField"){
        // Save created/setted form field to the database
        $setofCols = $_POST['setofCols'];
        $dataFieldSet = $_POST['dataFieldSet'];
        $companyID = $_POST['companyID'];
        $numCols = $_POST['numCols'];
        $tbset_field = $db->query("SELECT * FROM tbset_field WHERE companyID='$companyID'","row");
        if($tbset_field['companyID']==$companyID){
            $update = array("numCol"=>$numCols,
                            "colTitle"=>$setofCols,
                            "colValue_formID"=>$dataFieldSet,
                            "date_updated"=>date("Y-m-d  H:i:s"));
            $set = array("companyID"=>$companyID);
            $db->update("tbset_field",$update,$set);
            echo "Saved";
        }else{
            $insert = array("userID"=>$user_id,
                            "numCol"=>$numCols,
                            "colTitle"=>$setofCols,
                            "colValue_formID"=>$dataFieldSet,
                            "companyID"=>$companyID,
                            "date_updated"=>date("Y-m-d  H:i:s"),
                            "is_active"=>1);
            $db->insert("tbset_field",$insert);
            echo "Saved";
        }
        
        
    }
}

?>